/**
 * 观察者一共有四个组件：
 1:Subject: maintains a list of observers, facilitates adding or removing observers
   目标对象（类似接口，不具体实现，只有方法名）
 2:Observer: provides a update interface for objects that need to be notified of a Subject’s changes of state
   观察者对象 主要是update方法（类似接口，不具体实现，只有方法名）
 3:ConcreteSubject: broadcasts notifications to observers on changes of state, stores the state of ConcreteObservers
   具体目标对象，继承（实现）目标对象（实现接口）
 4:ConcreteObserver: stores a reference to the ConcreteSubject, implements an update interface for the Observer to ensure state is consistent with the Subject’s
   具体观察者，继承（实现）观察者（实现接口）
 https://blog.csdn.net/future_todo/article/details/53992152?utm_source=copy
 */

/*Subject 目标*/
class Subject {
  addObserver() {
    throw new Error("This method must be overwritten!");
  }
  removeObserver() {
    throw new Error("This method must be overwritten!");
  }
  notify() {
    throw new Error("This method must be overwritten!");
  }
}

class Observer {
  update() {
    throw new Error("This method must be overwritten!");
  }
}
//=============================================================
//具体的对象
class ControlCheckbox extends Subject {
  constructor() {
    super();
    this.observers = [];
  }
  addObserver(observer){
    this.observers.push(observer);
  }
  notify(context) {
    let observerCount = this.observers.length;
    for (let i = 0; i < observerCount; i++) {
      this.observers[i].update(context);
    }
  }
}


//具体的观察者
class AddedCheckboxs extends Observer{
  constructor(subject){
    super();
    console.log(subject);
    this.subject = subject;
    // this.subject.addObserver(this);
  }
  update(context){
    this.checked = context;
  }
}

